package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.nls.common.ConsoleConstant;
import com.fdb.efp.nls.service.facade.CtrLoanContService;
import com.fdb.efp.nls.service.vo.CtrLoanContVO;
import com.fdb.efp.flow.common.vo.PageApproveVO;
import com.fdb.efp.flow.service.facade.BizEventInfoService;
import com.fdb.efp.limit.service.facade.LmtPrdContService;
import com.fdb.efp.limit.service.vo.LmtPrdContVO;
import com.fdb.efp.nls.service.dao.LmtTerminateAppDao;
import com.fdb.efp.nls.service.domain.LmtTerminateApp;
import com.fdb.efp.nls.service.facade.LmtTerminateAppApprService;
import com.fdb.efp.nls.service.vo.LmtTerminateAppVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>ServiceImpl: 额度终止申请表				<p>
 * @author ligm
 * @date 2019-08-10
 */
@Service("lmtTerminateAppApprService")
public class LmtTerminateAppApprServiceImpl implements LmtTerminateAppApprService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(LmtTerminateAppApprServiceImpl.class);

	@Autowired
	private LmtTerminateAppDao lmtTerminateAppDao;

	@Autowired
	@Qualifier("lmtPrdContService")
	private LmtPrdContService lmtPrdContService;

	@Autowired
	@Qualifier("bizEventInfoService")
	private BizEventInfoService bizEventInfoService;

	@Override
	public void pass(String bizSerno, PageApproveVO pageApproveVo) {
		logger.info("额度终止通过流水号" + bizSerno);
		try {
			if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
				LmtTerminateApp vo = new LmtTerminateApp();
				vo.setTerminateSerno(bizSerno);
				LmtTerminateApp lmtTerminateApp = lmtTerminateAppDao.queryByPk(vo);
				LmtTerminateAppVO qvo = new LmtTerminateAppVO();
				beanCopy(lmtTerminateApp, qvo);
				if (Objects.nonNull(qvo)) {
					qvo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
					qvo.setLastUpdateUser(pageApproveVo.getAprvUserId());
					// 审批用户编号
					qvo.setAprvUserCode(pageApproveVo.getAprvUserId());
					qvo.setAprvUserName(pageApproveVo.getAprvUserName());
					// 审批意见
					qvo.setAprvComment(pageApproveVo.getAprvComment());
					// 审批状态 = 通过
					qvo.setApproveStatus(ConsoleConstant.APRV_STATUS_03);
					// 审批时间
					qvo.setAprvDate(TimeUtil.getCurrentDate());
				}
				beanCopy(qvo, lmtTerminateApp);
				lmtTerminateAppDao.updateByPk(lmtTerminateApp);

				// 更新授信协议信息表 修改 额度状态为‘已失效‘
				LmtPrdContVO lmtPrdContVO = new LmtPrdContVO();
				lmtPrdContVO.setLmtContNo(lmtTerminateApp.getLmtContNo());
				LmtPrdContVO out = lmtPrdContService.queryByPk(lmtPrdContVO);
				out.setLmtStatus(ConsoleConstant.LIMIT_STS_03);
				lmtPrdContService.updateByPk(out);

				// 更新授信合同表状态为注销
				CtrLoanContVO ctrLoanContVO = new CtrLoanContVO();
				ctrLoanContVO.setLmtContNo(lmtTerminateApp.getLmtContNo());
				ctrLoanContVO.setContType("01");
				ctrLoanContVO.setContState("300");
				ctrLoanContService.updateStateByLmtContNo(ctrLoanContVO);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void refuse(String bizSerno, PageApproveVO pageApproveVo) {
		logger.info("额度终止拒绝流水号" + bizSerno);
		try {
			if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
				LmtTerminateApp vo = new LmtTerminateApp();
				vo.setTerminateSerno(bizSerno);
				LmtTerminateApp lmtTerminateApp = lmtTerminateAppDao.queryByPk(vo);
				LmtTerminateAppVO qvo = new LmtTerminateAppVO();
				beanCopy(lmtTerminateApp, qvo);
				if (Objects.nonNull(qvo)) {
					qvo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
					qvo.setLastUpdateUser(pageApproveVo.getAprvUserId());
					// 审批用户编号
					qvo.setAprvUserCode(pageApproveVo.getAprvUserId());
					qvo.setAprvUserName(pageApproveVo.getAprvUserName());
					// 审批意见
					qvo.setAprvComment(pageApproveVo.getAprvComment());
					// 审批状态 = 拒绝
					qvo.setApproveStatus(ConsoleConstant.APRV_STATUS_04);
					// 审批时间
					qvo.setAprvDate(TimeUtil.getCurrentDate());
				}
				beanCopy(qvo, lmtTerminateApp);
				lmtTerminateAppDao.updateByPk(lmtTerminateApp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	@Override
	public void repulse(String bizSerno, PageApproveVO pageApproveVo) {
		try {
			if (bizEventInfoService.isFirstNode(bizSerno)) {
				logger.info("额度终止打回流水号" + bizSerno);
				if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
					LmtTerminateApp vo = new LmtTerminateApp();
					vo.setTerminateSerno(bizSerno);
					LmtTerminateApp lmtTerminateApp = lmtTerminateAppDao.queryByPk(vo);
					LmtTerminateAppVO qvo = new LmtTerminateAppVO();
					beanCopy(lmtTerminateApp, qvo);
					if (Objects.nonNull(qvo)) {
						qvo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
						qvo.setLastUpdateUser(pageApproveVo.getAprvUserId());
						// 审批用户编号
						qvo.setAprvUserCode(pageApproveVo.getAprvUserId());
						qvo.setAprvUserName(pageApproveVo.getAprvUserName());
						// 审批意见
						qvo.setAprvComment(pageApproveVo.getAprvComment());
						// 审批状态 = 打回
						qvo.setApproveStatus(ConsoleConstant.APRV_STATUS_05);
						// 审批时间
						qvo.setAprvDate(TimeUtil.getCurrentDate());
					}
					beanCopy(qvo, lmtTerminateApp);
					lmtTerminateAppDao.updateByPk(lmtTerminateApp);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Autowired
	@Qualifier("ctrLoanContService")
	private CtrLoanContService ctrLoanContService;
}
